home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bc_ti.zip / TI353.ASC next >
Text File  |  1992-02-25  |  8KB  |  331 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Turbo C                                NUMBER  :  353
  9.   VERSION  :  2.0
  10.        OS  :  DOS
  11.      DATE  :  February 25, 1992                        PAGE  :  1/4
  12.  
  13.     TITLE  :  Creating a New Data Segment
  14.  
  15.  
  16.  
  17.  
  18.   Sometimes people have  the  need  to  allow  more  than  one data
  19.   segment in their program and they can not change to a HUGE memory
  20.   model. This technical information handout explains what is needed
  21.   to  add  an  extra data segment to your Turbo C program. You will
  22.   need an Assembler to reassemble the start-up code.
  23.  
  24.   The first change you will need to make is to the Turbo C start-up
  25.   code. This code defines the segments and their classes as well as
  26.   their group. To define a new segment in your  program,  you  will
  27.   need to add the definition of the new segment and its class. This
  28.   definition must be placed before all of the  other  segments that
  29.   the  start-up  code  defines.  See  the following listing  as  an
  30.   example of the existing  start-up  code. The bold text is the new
  31.   segment definition that we are adding.
  32.  
  33.           NAME    c0
  34.           PAGE    60,132
  35.   ;[]------------------------------------------------------------[]
  36.   ;|      C0.ASM -- Start Up Code                                 |
  37.   ;|                                                              |
  38.   ;|      Turbo-C Run Time Library        version 2.0             |
  39.   ;|                                                              |
  40.   ;|      Copyright (c) 1988 by Borland International Inc.        |
  41.   ;|      All Rights Reserved.                                    |
  42.   ;[]------------------------------------------------------------[]
  43.  
  44.           INCLUDE RULES.ASI
  45.  
  46.   _Strict87_      equ     false           ; emulation skips peculiar details
  47.  
  48.   ;       Segment and Group declarations
  49.  
  50.   _MYSEG  SEGMENT BYTE PUBLIC 'NEWSEG'       ; Definition of new
  51.   _MYSEG  ENDS                               ; Data Segment
  52.   _TEXT   SEGMENT BYTE PUBLIC 'CODE'
  53.   _TEXT   ENDS
  54.   _DATA   SEGMENT PARA PUBLIC 'DATA'
  55.   _DATA   ENDS
  56.                           Figure 1: c0.asm
  57.  
  58.   Now that you  have  changed  the  start-up code, you will need to
  59.   reassemble  it  for  the  memory  model that you need. The  files
  60.   RULES.ASI  and  EMUVARS.ASI  will  need  to  be  in your  current
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Turbo C                                NUMBER  :  353
  75.   VERSION  :  2.0
  76.        OS  :  DOS
  77.      DATE  :  February 25, 1992                        PAGE  :  2/4
  78.  
  79.     TITLE  :  Creating a New Data Segment
  80.  
  81.  
  82.  
  83.  
  84.   directory.  The  batch  file,  BUILD-C0.BAT,  should  be  used to
  85.   reassemble  the  start-up  code.  It  takes  as  a  command  line
  86.   parameter the  model name (TINY, SMALL, MEDIUM, COMPACT, LARGE, &
  87.   HUGE). Place the modified assembled start-up code in your project
  88.   directory.
  89.  
  90.   Now that we have changed the start-up code, we  need  to  compile
  91.   the  .C file that will be using the data segment. We will use the
  92.   following .C module as an example.
  93.  
  94.   char c_buffer1[30000];
  95.   char c_buffer2[30000];
  96.                          Figure 2: module1.c
  97.  
  98.   There are several compiler directives that we will  need  to pass
  99.   to TCC to compile this module for the other segment. Following is
  100.   the command line used:
  101.  
  102.   TCC -m? -c -v -zBnewseg -zD_myseg -zGnewgroup -zR_myseg
  103.       -zSnewgroup -zTnewseg module1.c
  104.  
  105.   The -m? option is used to specify the memory model for the module
  106.   to  be compiled in. The ? character should  be  replaced  by  the
  107.   letter  for  the  model that you wish to use. The -c option tells
  108.   the  compiler  not  to invoke the  linker.  Using  -v  tells  the
  109.   compiler  to  include  full debug information. The  rest  of  the
  110.   directives specify the names of the segment, class, and group for
  111.   both the DATA area and the BSS area. These different  options are
  112.   discussed in the Turbo C Reference Guide in Appendix C  under the
  113.   section  Segment-Naming Control. Essentially, _myseg is the  name
  114.   of the segment. This  name  should match what was placed into the
  115.   start-up code. Newseg is  the  name of the class for the segments
  116.   and should correspond to the name in '' in the start-up code. The
  117.   newgroup is  the  name  of  the  group  that the segments will be
  118.   placed in.
  119.  
  120.   The other module that we are using in our example follows  in the
  121.   next  figure.  This  module  will  need to specify  the  external
  122.   variables with the far key word. Please keep in mind that  if you
  123.   are in one  of  the  near  data  memory  models  (TINY,SMALL, and
  124.   MEDIUM) that the functions by default only take an  offset  as  a
  125.   parameter. The segment used to  reference this offset is the data
  126.   segment (DS) by default. If you want to use the external  data in
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Turbo C                                NUMBER  :  353
  141.   VERSION  :  2.0
  142.        OS  :  DOS
  143.      DATE  :  February 25, 1992                        PAGE  :  3/4
  144.  
  145.     TITLE  :  Creating a New Data Segment
  146.  
  147.  
  148.  
  149.  
  150.   a Turbo C RTL function, you will need to copy the  information to
  151.   a local variable using the  function  movedata().  Otherwise, the
  152.   COMPACT, LARGE, and HUGE memory models will work fine as  long as
  153.   you specify that the external variables are far.
  154.  
  155.   #include <dos.h>
  156.   #include <stdio.h>
  157.  
  158.   extern char far c_buffer1[30000];
  159.   extern char far c_buffer2[30000];
  160.   char c_buffer3[30000];
  161.   char c_buffer4[30000];
  162.  
  163.   void show_segment(void far *ptr);
  164.  
  165.   main()
  166.   {
  167.     printf("local proc\n");
  168.     printf("extern data ");
  169.     show_segment(c_buffer1);
  170.     printf("local data ");
  171.     show_segment(c_buffer3);
  172.  
  173.     return 0;
  174.   }
  175.  
  176.   void show_segment(void far *ptr)
  177.   {
  178.           printf("segment:%04X\n",FP_SEG(ptr));
  179.   }
  180.                          Figure 3: module2.c
  181.  
  182.   This  module  can  be compiled with a simple command line (again,
  183.   remembering to replace  the  ?  with  the  letter  of  the memory
  184.   model):
  185.  
  186.   TCC -m? -c -v module2.c
  187.  
  188.   Now that  each  of  the  modules  are  compiled, we can link them
  189.   together.  We  are  calling  TLINK  directly because we  want  to
  190.   specify the location of the start-up code. If we  pass  the  .OBJ
  191.   files to TCC, it may use the wrong start-up code. The  TLINK line
  192.   is as follows (replacing the ? with letter of the memory model):
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Turbo C                                NUMBER  :  353
  207.   VERSION  :  2.0
  208.        OS  :  DOS
  209.      DATE  :  February 25, 1992                        PAGE  :  4/4
  210.  
  211.     TITLE  :  Creating a New Data Segment
  212.  
  213.  
  214.  
  215.  
  216.   TLINK /v /c c0? module1 module2, example, example, \tc\lib\c?
  217.  
  218.   The  resulting executable program (EXAMPLE.EXE) will display  the
  219.   segments of the default segment global variables and the external
  220.   segment global variables.
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.   PRODUCT  :  Turbo C                                NUMBER  :  353
  273.   VERSION  :  2.0
  274.        OS  :  DOS
  275.      DATE  :  February 25, 1992                        PAGE  :  1/4
  276.  
  277.     TITLE  :  Creating a New Data Segment
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.                             Figures
  289.  
  290.  
  291.        Figure 1: c0.asm  . . . . . . . . . . . . . . . . . . 1
  292.        Figure 2: module1.c . . . . . . . . . . . . . . . . . 2
  293.        Figure 3: module2.c . . . . . . . . . . . . . . . . . 3
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.